package com.qyer.search.threading;/**
 * User: Z J Wu Date: 2016/01/04 Time: 17:04 Package: com.qyer.search.threading
 */

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;

public class BasicWordMappingThreadPoolHolder extends AbstractThreadPoolHolder {

  private static final Logger LOGGER = LoggerFactory.getLogger(BasicWordMappingThreadPoolHolder.class);

  private static class InnerHolder {

    private static final BasicWordMappingThreadPoolHolder INSTANCE = new BasicWordMappingThreadPoolHolder();
  }

  private BasicWordMappingThreadPoolHolder() {
    CompositeConfiguration c = new CompositeConfiguration();
    int wordLoaderCount = 20;
    String poolName = "BasicWordMapping";
    try {
      c.addConfiguration(new PropertiesConfiguration("words_config.properties"));
      wordLoaderCount = c.getInt("wordetl.workerThreadCount", 50);
      poolName = c.getString("wordetl.threadPoolName", "word-etl");
    } catch (ConfigurationException e) {
      LOGGER.error("Cannot load config.", e);
    }
    LOGGER.info("Basic word mapping thread pool({} threads) has been built.", wordLoaderCount);
    this.name = poolName;
    this.service = Executors
      .newFixedThreadPool(wordLoaderCount, new GenericThreadThreadFactory(poolName));
  }

  public static final BasicWordMappingThreadPoolHolder getInstance() {
    return InnerHolder.INSTANCE;
  }

}
